// packageE/appointment/technician/appointment_project/appointment_project.js
var app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    active: 0,
    url: "",
    projectList: [],
    title: '已预约项目',

    // more
    isLoadMore: true,
    page: 1,
    total_page: 0,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.init();
    this.getData();
  },
  selectTab(e) {
    console.log(e);
    let ind = e.detail.index;
    this.setData({
      active: ind != 2 ? ind : 9,
    });
    this.getData();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {},

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if (this.data.current_page >= this.data.last_page) {
      // console.log('没有更多');
      return;
    }else{
      let pages = this.data.page + 1;
      this.getMoreData(pages);
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {},
  init() {
    this.page = 1;
    this.isLoadMore = true;
    this.total_page = 0;
    this.projectList = [];
    this.setData({
      page: 1,
      isLoadMore: true,
      total_page: 0,
      projectList: [],
      appointmentLang: wx.getStorageSync("yz_basic_info").lang.appointment,
    });
    if (this.options.name === "store") {
      this.setData({
        title: "门店预约",
        url:
          "plugin.appointment.frontend.appointment-service.get-list-by-store",
      });
    } else {
      this.setData({
        title: this.data.appointmentLang.worker + "预约",
        url:
          "plugin.appointment.frontend.appointment-service.get-list-by-worker",
      });
    }
    wx.setNavigationBarTitle({
      title: this.data.title,
    });
  },
  sure(e){
    let id = e.currentTarget.dataset.id;
    wx.showModal({
      title:"您确定要取消此预约吗?",
      success:(res)=>{
        if(res.confirm){
          this.cancelDate(id);
        }
      }
    });
  },
  cancelDate(kid){
    let urlStr = app.getNetAddresss("plugin.appointment.frontend.appointment-service.cancel-service");
    app._getNetWork({
      url: urlStr,
      data: {
        id: kid
      },
      success: (resdata) => {
        var res = resdata.data;
        if (res.result == 1) {
          app.tips(res.msg);
          this.getData();
        } else {
          wx.showToast({
            title: res.msg,
            icon: "none",
          });
        }
      },
    });
  },
  getData() {
    this.init();
    let that = this;
    let urlStr = app.getNetAddresss(this.data.url);
    app._getNetWork({
      url: urlStr,
      data: {
        status: that.data.active,
        page: that.data.page,
      },
      success: (resdata) => {
        var res = resdata.data;
        if (res.result == 1) {
          console.log(res);
          that.setData({
            projectList: res.data.data,
            current_page: res.data.current_page,
            last_page: res.data.last_page,
          });
        } else {
          wx.showToast({
            title: res.msg,
            icon: "none",
          });
        }
      },
    });
  },
  getMoreData(pages) {
    let that = this;
    let urlStr = app.getNetAddresss(this.data.url);
    app._getNetWork({
      url: urlStr,
      data: {
        status: that.data.active,
        page: pages,
      },
      success: (resdata) => {
        var res = resdata.data;
        if (res.result == 1) {
          that.setData({
            projectList: this.data.projectList.concat(res.data.data),
            current_page: res.data.current_page,
            last_page: res.data.last_page,
          });
        } else {
          wx.showToast({
            title: res.msg,
            icon: "none",
          });
        }
      },
    });
  },
});
